package com.zookeeper.case2;

import org.apache.zookeeper.KeeperException;

import java.io.IOException;

/**
 * @author WangGuoLiang
 * @program ZooKeeper
 * @description 分布式锁测试
 * @create 2021-09-05 11:38
 **/
public class DistributeLockTest {
    public static void main(String[] args) throws InterruptedException, IOException, KeeperException {
        DistributeLock lock = new DistributeLock();
        DistributeLock lock1 = new DistributeLock();


        new Thread(){
            @Override
            public void run() {
               lock.zkLock();
                System.out.println("线程1启动 获得到锁");
                try {
                    Thread.sleep(5000);
                    lock.unZkLock();
                    System.out.println("线程1释放锁");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                lock1.zkLock();
                System.out.println("线程2启动 获得到锁");
                try {
                    Thread.sleep(5000);
                    lock1.unZkLock();
                    System.out.println("线程2释放锁");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}

